#coding:utf-8
import getpass,pickle
def login():
    count = 0
    while count < 3:
        _user = raw_input('请输入用户名:')
        _password = getpass.getpass('请输入您的密码:')
        try:
            with open('e:/password.pkl','rb') as f:
                dict = pickle.load(f)
                if dict.has_key(_user):
                    if dict[_user] != _password:
                        print '密码错误！'
                        count += 1
                        continue
                    print 'OK!'
                    break
                else:
                    print '用户名不存在！'
                    count += 1
                    continue
        except:
            print '文件错误！'
    else:
        print '超过三次，locked!'



def register():

    Flag = True

    while Flag:
        _reg_user = raw_input("请输入您需要注册的用户名:")
        try:
            with open('e:/password.pkl','ab+') as f:
                try:
                    dict = pickle.load(f)
                    print dict
                    if dict.has_key(_reg_user):
                        print '用户名已经存在请重新输入注册名：'
                        continue
                except:
                    pass
        except IOError,e:
            print '文件错误'
        _reg_password = getpass.getpass('输入你的密码：')
        _reg_pas_again = getpass.getpass('两次密码验证：')
        if _reg_password != _reg_pas_again:
            print '您两次输入的密码不一致，请重新注册！'
            continue
        try:
            with open('e:/password.pkl','rb+') as f:
                try:
                    dict1=pickle.load(f)
                    dict1[_reg_user]=_reg_pas_again
                    f.seek(0)
                    pickle.dump(dict1,f)
                except :
                    dict1={}
                    dict1[_reg_user]=_reg_pas_again
                    pickle.dump(dict1, f)
        except Exception as e:
            raise e
        Flag = False



if __name__ == '__main__':
    login()
    ...